﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GTServices.WPF.Client.ViewModels.Entities;
using Caliburn.Micro;
using GTServices.Model;
using GTServices.Entities;

namespace GTServices.WPF.Client.ViewModels
{
    public class MatchInfoDetailViewModel : Screen
    {
        private GrepResultsTreeViewModel _parentViewModel;

        public GrepResultsTreeViewModel ParentViewModel
        {
            get { return _parentViewModel; }
            set
            {
                _parentViewModel = value;
                NotifyOfPropertyChange(() => ParentViewModel);
            }
        }

        private MatchInfo _matchInfo;
        public MatchInfo MatchInfo
        {
            get
            {
                return _matchInfo;
            }

            set
            {
                _matchInfo = value;
               
                if (value != null)
                {
                    if(FileMatchs != null)
                        SelectedFileMatchInfo = FileMatchs.FirstOrDefault(x => x.MatchInfo == value);
                    if(_matchInfo.FileInfo != null)
                        DisplayName = _matchInfo.FileInfo.FullName;
                }
                NotifyOfPropertyChange(() => FileMatchs); 
                NotifyOfPropertyChange(() => MatchInfo); 
            }
        }

        private MatchInfoViewModel _selectedFileMatchInfo;
        public MatchInfoViewModel SelectedFileMatchInfo
        {
            get { return _selectedFileMatchInfo; }
            set
            {
                _selectedFileMatchInfo = value;
                
                if(value != null)
                    Editor.MatchInfo = value.MatchInfo;
                NotifyOfPropertyChange(() => SelectedFileMatchInfo); 
            }
        }



        private BindableCollection<MatchInfoViewModel> _fileMatchs;
        
        /// <summary>
        /// Related File Matches
        /// </summary>
        public BindableCollection<MatchInfoViewModel> FileMatchs
        {
            get
            {
                return _fileMatchs;
            }

            set
            {
                _fileMatchs = value;
                NotifyOfPropertyChange(() => FileMatchs); 
            }
        }

        private MatchInfoEditorViewModel _editor; 
        public MatchInfoEditorViewModel Editor { 
            get {
                return _editor; 
            }
            set{
                _editor = value;
                NotifyOfPropertyChange(() => Editor); 
            }
        } 


        public MatchInfoDetailViewModel()
        {
            Editor = new MatchInfoEditorViewModel(); 
        }


        public void Remove()
        {
            if (this.SelectedFileMatchInfo != null)
            {
                GrepResultTreeViewItemViewModel grvm = ParentViewModel.RootGrepResults.FirstOrDefault(x => x.GrepResult.GrepRequest == this.SelectedFileMatchInfo.MatchInfo.GrepRequest);

                grvm.GrepResult.MatchInfos.Remove(this.SelectedFileMatchInfo.MatchInfo);
                foreach (CategoryTreeViewItemViewModel itvi in grvm.Children)
                {
                    itvi.RefreshChildren(); 
                }
                FileMatchs.Remove(this.SelectedFileMatchInfo); 

                this.SelectedFileMatchInfo = null;
                NotifyOfPropertyChange(() => FileMatchs);
                NotifyOfPropertyChange(() => this.SelectedFileMatchInfo); 
            }
        }

    }
}
